package com.txunda.zhuohired.aty.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ants.theantsgo.config.Settings;
import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.imageLoader.GlideImageLoader;
import com.ants.theantsgo.tool.ToolKit;
import com.ants.theantsgo.util.CompressionUtil;
import com.ants.theantsgo.util.L;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.smarttop.library.bean.City;
import com.smarttop.library.bean.County;
import com.smarttop.library.bean.Province;
import com.smarttop.library.bean.Street;
import com.smarttop.library.db.manager.AddressDictManager;
import com.smarttop.library.utils.LogUtil;
import com.smarttop.library.widget.AddressSelector;
import com.smarttop.library.widget.BottomDialog;
import com.smarttop.library.widget.OnAddressSelectedListener;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.base.Base2Aty;
import com.txunda.zhuohired.bean.manage.IndustryBean;
import com.txunda.zhuohired.bean.manage.ScaleBean;
import com.txunda.zhuohired.dialog.IndustryDialog;
import com.txunda.zhuohired.dialog.ScaleDialog;
import com.txunda.zhuohired.http.pst.CompanyPst;
import com.txunda.zhuohired.http.pst.MemberPst;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/10 11:19
 * 功能描述：企业认证
 * 联系方式：jiahuanchen@126.com
 */

public class CompanyApproveAty extends Base2Aty implements AddressSelector.onSelectorAreaPositionListener,
        OnAddressSelectedListener, AddressSelector.OnDialogCloseListener {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_blue_center)
    private TextView tv_layout_blue_center;
    /**
     * 企业logo
     */
    @ViewInject(R.id.iv_company_approve_logo)
    private ImageView iv_company_approve_logo;
    /**
     * 企业环境
     */
    @ViewInject(R.id.iv_company_approve_environment)
    private ImageView iv_company_approve_environment;
    /**
     * 地址
     */
    @ViewInject(R.id.tv_company_approve_address)
    private TextView tv_company_approve_address;
    /**
     * 行业
     */
    @ViewInject(R.id.tv_company_approve_industry)
    private TextView tv_company_approve_industry;
    /**
     * 规模
     */
    @ViewInject(R.id.tv_company_approve_scale)
    private TextView tv_company_approve_scale;
    /**
     * 企业介绍
     */
    @ViewInject(R.id.tv_company_approve_introduce)
    private TextView tv_company_approve_introduce;
    /**
     * 公司名称
     */
    @ViewInject(R.id.et_company_name)
    private EditText et_company_name;
    /**
     * 公司地址
     */
    @ViewInject(R.id.et_company_address)
    private EditText et_company_address;
    /**
     * 门牌号
     */
    @ViewInject(R.id.et_company_num)
    private EditText et_company_num;
    /**
     * 行政电话
     */
    @ViewInject(R.id.et_company_phone)
    private EditText et_company_phone;

    private File file;
    private File file1;
    private int size = 0;

    private BottomDialog dialog;
    private int provincePosition;
    private int cityPosition;
    private int countyPosition;
    private int streetPosition;
    private String provinceCode = "";//省id
    private String cityCode = "";//市id
    private String countyCode = "";//区id
    private String streetCode = "";//

    private MemberPst memberPst;
    private AddressDictManager addressDictManager;
    private List<IndustryBean.DataBean> industryList;
    private String industry_id = "";//行业id
    private List<ScaleBean.DataBean> scaleList;
    private String scale_id = "";//规模id
    private String company_introduce = "";//公司介绍
    private String token = "";
    private CompanyPst companyPst;
    private String company_name = "";//公司名称
    private String company_address = "";//公司地址
    private String company_num = "";//门牌号
    private String company_phone = "";//行政电话

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_blue_head);
        tv_layout_blue_center.setText("企业认证");
    }

    @Override
    @OnClick({R.id.iv_company_approve_logo, R.id.iv_company_approve_environment, R.id.tv_company_approve_next, R.id
            .ll_company_approve_introduce, R.id.ll_company_approve_address, R.id.ll_company_approve_industry, R.id
            .ll_company_approve_scale})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_company_approve_logo://企业logo
                startActivityForResult(ImageGridActivity.class, null, 100);
                break;
            case R.id.iv_company_approve_environment://企业环境
                startActivityForResult(ImageGridActivity.class, null, 101);
                break;
            case R.id.tv_company_approve_next://下一步
                //                startActivity(CompanyAptitudeAty.class, null);
                company_name = et_company_name.getText().toString().trim();
                company_address = et_company_address.getText().toString().trim();
                company_num = et_company_num.getText().toString().trim();
                company_phone = et_company_phone.getText().toString().trim();
                if (TextUtils.isEmpty(company_name)) {
                    showErrorTip("请输入企业名称");
                    return;
                }
                if (TextUtils.isEmpty(company_name)) {
                    showErrorTip("请输入企业名称");
                    return;
                }
                if (TextUtils.isEmpty(cityCode)) {
                    showErrorTip("请选择地址");
                    return;
                }
                if (TextUtils.isEmpty(company_address)) {
                    showErrorTip("请选择地址");
                    return;
                }
                if (TextUtils.isEmpty(countyCode)) {
                    showErrorTip("请选择地址");
                    return;
                }
                if (TextUtils.isEmpty(company_num)) {
                    showErrorTip("请输入门牌号");
                    return;
                }
                if (TextUtils.isEmpty(company_phone)) {
                    showErrorTip("请输入行政电话");
                    return;
                }
                if (TextUtils.isEmpty(provinceCode)) {
                    showErrorTip("请选择企业地址");
                    return;
                }
                if (TextUtils.isEmpty(company_introduce)) {
                    showErrorTip("请编辑企业介绍");
                    return;
                }
                if (TextUtils.isEmpty(industry_id)) {
                    showErrorTip("请选择行业");
                    return;
                }
                if (TextUtils.isEmpty(scale_id)) {
                    showErrorTip("请选择规模");
                    return;
                }
                if (file == null) {
                    showErrorTip("请选择企业LOGO");
                    return;
                }
                if (file1 == null) {
                    showErrorTip("请选择企业环境");
                    return;
                }
                companyPst.companyIn(token, company_name, industry_id, scale_id, provinceCode, cityCode, countyCode,
                        company_introduce, company_phone, file, file1, company_address, company_num);

                break;
            case R.id.ll_company_approve_introduce://企业介绍
                startActivityForResult(CompanyApproveIntroduceAty.class, null, 102);
                break;
            case R.id.ll_company_approve_address://企业地址

                if (dialog != null) {
                    dialog.show();
                } else {
                    dialog = new BottomDialog(this);
                    dialog.setOnAddressSelectedListener(this);
                    dialog.setDialogDismisListener(this);
                    dialog.setTextSize(14);//设置字体的大小
                    dialog.setIndicatorBackgroundColor(android.R.color.holo_orange_light);//设置指示器的颜色
                    dialog.setTextSelectedColor(android.R.color.holo_orange_light);//设置字体获得焦点的颜色
                    dialog.setTextUnSelectedColor(android.R.color.holo_blue_light);//设置字体没有获得焦点的颜色
                    //            dialog.setDisplaySelectorArea("31",1,"2704",1,"2711",0,"15582",1);//设置已选中的地区
                    dialog.setSelectorAreaPositionListener(this);
                    dialog.show();
                }
                break;
            case R.id.ll_company_approve_industry://行业
                IndustryDialog industryDialog = new IndustryDialog(this, industryList, new IndustryDialog
                        .SelectStationListener() {
                    @Override
                    public void onSelectStation(String type, String id) {
                        tv_company_approve_industry.setText(type);
                        industry_id = id;
                    }
                });
                industryDialog.show();
                break;
            case R.id.ll_company_approve_scale://规模
                ScaleDialog scaleDialog = new ScaleDialog(this, scaleList, new ScaleDialog.ScaleListener() {
                    @Override
                    public void onScale(String type, String id) {
                        tv_company_approve_scale.setText(type);
                        scale_id = id;
                    }
                });
                scaleDialog.show();
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_company_approve;
    }

    @Override
    protected void initialized() {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());
        imagePicker.setShowCamera(true);// 显示相机
        imagePicker.setMultiMode(false);
        imagePicker.setCrop(true);
        imagePicker.setFocusHeight(Settings.displayWidth);
        imagePicker.setFocusWidth(Settings.displayWidth);
        imagePicker.setOutPutX(Settings.displayWidth);
        imagePicker.setOutPutY(Settings.displayWidth);
        size = ToolKit.dip2px(this, 68);
        memberPst = new MemberPst(this);
        companyPst = new CompanyPst(this);
        addressDictManager = new AddressDictManager(this);
        industryList = new ArrayList<>();
        scaleList = new ArrayList<>();
        token = application.getUserInfo().get("data");
    }

    @Override
    protected void requestData() {
        //        memberPst.region();//地址
        memberPst.trade();//行业
        companyPst.scale();//规模
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker
                        .EXTRA_RESULT_ITEMS);
                String pic_path = CompressionUtil.compressionBitmap(images.get(0).path);
                switch (requestCode) {
                    case 100:
                        file = new File(pic_path);
                        Glide.with(this).load(file).override(size, size).centerCrop().into(iv_company_approve_logo);
                        break;
                    case 101:
                        file1 = new File(pic_path);
                        Glide.with(this).load(file1).override(size, size).centerCrop().into
                                (iv_company_approve_environment);
                        break;
                }
            } else {
                Toast.makeText(this, "加载出错", Toast.LENGTH_SHORT).show();
            }
        } else if (RESULT_OK == resultCode) {
            if (requestCode == 102) {//企业介绍
                L.e("========", "测试");
                company_introduce = data.getStringExtra("company_introduce");
                tv_company_approve_introduce.setText(company_introduce);
            }
        }
    }

    @Override
    public void selectorAreaPosition(int provincePosition, int cityPosition, int countyPosition, int streetPosition) {
        this.provincePosition = provincePosition;
        this.cityPosition = cityPosition;
        this.countyPosition = countyPosition;
        this.streetPosition = streetPosition;
        LogUtil.d("数据", "省份位置=" + provincePosition);
        LogUtil.d("数据", "城市位置=" + cityPosition);
        LogUtil.d("数据", "乡镇位置=" + countyPosition);
        LogUtil.d("数据", "街道位置=" + streetPosition);
    }

    @Override
    public void onAddressSelected(Province province, City city, County county, Street street) {
        provinceCode = (province == null ? "" : province.code);
        cityCode = (city == null ? "" : city.code);
        countyCode = (county == null ? "" : county.code);
        streetCode = (street == null ? "" : street.code);
        L.e("=================", "省份id=" + provinceCode);
        L.e("=================", "城市id=" + cityCode);
        L.e("=================", "乡镇id=" + countyCode);
        L.e("=================", "街道id=" + streetCode);
        String s = (province == null ? "" : province.name) + (city == null ? "" : city.name) + (county == null ? "" :
                county.name) +
                (street == null ? "" : street.name);
        L.e("=================", s);
        tv_company_approve_address.setText(s);
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    @Override
    public void dialogclose() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("scale")) {
            L.e("======scale====", jsonStr);
            ScaleBean scaleBean = GsonUtil.GsonToBean(jsonStr, ScaleBean.class);
            scaleList = scaleBean.getData();
            return;
        }
        if (requestUrl.contains("trade")) {
            L.e("======trade====", jsonStr);
            IndustryBean industryBean = GsonUtil.GsonToBean(jsonStr, IndustryBean.class);
            industryList = industryBean.getData();
            return;
        }
        if (requestUrl.contains("companyIn")) {
            L.e("=====companyIn=====", jsonStr);
            startActivity(CompanyAptitudeAty.class, null);
            return;
        }
    }
}
